package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.QueryBaseParam;
import com.example.demo.domain.entity.TrainClass;
import com.example.demo.domain.entity.TrainClassUser;
import com.example.demo.domain.model.ClassUserCount;
import com.example.demo.domain.vo.ClassUserSubmitInfoVo;
import com.example.demo.param.TrainClassUserParam;
import com.example.demo.service.TrainClassUserService;
import com.example.demo.mapper.TrainClassUserMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author liguang
* @description 针对表【train_class_user(培训系统-班级-用户关系)】的数据库操作Service实现
* @createDate 2023-11-23 21:10:29
*/
@Service
public class TrainClassUserServiceImpl extends ServiceImpl<TrainClassUserMapper, TrainClassUser>
    implements TrainClassUserService{

    @Resource
    TrainClassUserMapper classUserMapper;

    @Override
    public Page<TrainClassUser> queryByParam(TrainClassUserParam param) {
        if (param == null) {
            return null;
        }
        QueryWrapper<TrainClassUser> queryWrapper = new QueryWrapper<>();
        if (param.getTrainClassId() != null) {
            queryWrapper.eq("class_id", param.getTrainClassId());
        }
        queryWrapper.orderByDesc("user_id");
        Page<TrainClassUser> page = new Page<>(param.getPageNumber(), param.getPageSize());
        return classUserMapper.selectPage(page,queryWrapper);
    }

    @Override
    public boolean removeUser(Long trainClassId, Long userId) {
        QueryWrapper<TrainClassUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", trainClassId);
        queryWrapper.eq("user_id", userId);
        int delete = classUserMapper.delete(queryWrapper);
        return delete > 0;
    }

    @Override
    public List<TrainClassUser> getByUserId(Long userId) {
        QueryWrapper<TrainClassUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return classUserMapper.selectList(queryWrapper);
    }



    @Override
    public TrainClassUser queryByUserAndClass(Long trainClassId, Long id) {
        QueryWrapper<TrainClassUser> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("class_id", trainClassId).eq("user_id", id);
        return classUserMapper.selectOne(queryWrapper);
    }

    @Override
    public List<TrainClassUser> getAllUserByClassId(Long trainClassId) {
        QueryWrapper<TrainClassUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", trainClassId);
        return classUserMapper.selectList(queryWrapper);
    }

    @Override
    public Page<ClassUserSubmitInfoVo> getSubmitDetail(TrainClassUserParam trainClassUserParam) {
        int pageNumber = trainClassUserParam.getPageNumber();
        int offset = trainClassUserParam.getOffset();
        int pageSize = trainClassUserParam.getPageSize();
        Page<ClassUserSubmitInfoVo> result = new Page<>();
        result.setCurrent(pageNumber);
        result.setSize(pageSize);

        classUserMapper.getSubmitDetail(trainClassUserParam.getTrainClassId(), trainClassUserParam.getHomeworkId(),
                offset, pageSize);
        return null;
    }

    @Override
    public Page<TrainClass> queryClassByUserId(Long userId, Integer pageNumber, Integer pageSize) {
        QueryBaseParam param = new QueryBaseParam(pageNumber,pageSize);
        int offset = param.getOffset();
        int pageSize1 = param.getPageSize();
        Page<TrainClass> page = new Page<>();
        page.setCurrent(pageNumber);
        page.setSize(pageSize1);
        Long total = classUserMapper.countUserClasses(userId);
        page.setTotal(total);
        if (total == 0){
            return page;
        }
        List<TrainClass> userClasses = classUserMapper.getUserClasses(userId, offset, pageSize1);
        page.setRecords(userClasses);
        return page;
    }

    @Override
    public Page<TrainClass> queryClassByRealName(String realName, Integer pageNumber, Integer pageSize) {
        QueryBaseParam param = new QueryBaseParam(pageNumber,pageSize);
        int offset = param.getOffset();
        int pageSize1 = param.getPageSize();
        Page<TrainClass> page = new Page<>();
        page.setCurrent(pageNumber);
        page.setSize(pageSize1);
        Long total = classUserMapper.countUserClassesByRealName(realName);
        page.setTotal(total);
        if (total == 0){
            return page;
        }
        List<TrainClass> userClasses = classUserMapper.getUserClassesByRealName(realName, offset, pageSize1);
        page.setRecords(userClasses);
        return page;
    }

    @Override
    public  List<ClassUserCount> countNumber(List<Long> collect) {
        if (CollectionUtils.isEmpty(collect)){
            return new ArrayList<>();
        }
        List<ClassUserCount> classUserCounts = classUserMapper.countUserByClassIds(collect);

        return classUserCounts;
    }




}




